import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ToolBox extends JPanel implements MouseListener {

	public Canvas parent;
	private JLabel selectLabel, lineLabel, rectLabel, ovalLabel, eraserLabel, fillLabel;
	private ImageIcon selectIcon, lineIcon, rectIcon, ovalIcon, eraserIcon, fillIcon;
	private JLabel prevLabel;
	private int w, h;
	public int status;
	
	public ToolBox() {
		setLayout(new GridLayout(6,0));
		
		selectIcon = new ImageIcon("cursor_icon.gif");
		lineIcon = new ImageIcon("line_icon.gif");
		rectIcon = new ImageIcon("shapeRect_icon.gif");
		ovalIcon = new ImageIcon("shapeOval_icon.gif");
		eraserIcon = new ImageIcon("eraser_icon.gif");
		fillIcon = new ImageIcon("fill_icon.gif");
		
		w = selectIcon.getIconWidth();
		h = selectIcon.getIconHeight();
		
		selectLabel = new JLabel(selectIcon);
		selectLabel.setPreferredSize(new Dimension(w,h));
		selectLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
		selectLabel.setToolTipText("Select");
		selectLabel.addMouseListener(this);
		add(selectLabel);
		
		lineLabel = new JLabel(lineIcon);
		lineLabel.setPreferredSize(new Dimension(w,h));
		lineLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
		lineLabel.setToolTipText("Draw a Line");
		lineLabel.addMouseListener(this);
		add(lineLabel);
		
		rectLabel = new JLabel(rectIcon);
		rectLabel.setPreferredSize(new Dimension(w,h));
		rectLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
		rectLabel.setToolTipText("Draw a Rectangle");
		rectLabel.addMouseListener(this);
		add(rectLabel);
		
		ovalLabel = new JLabel(ovalIcon);
		ovalLabel.setPreferredSize(new Dimension(w,h));
		ovalLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
		ovalLabel.setToolTipText("Draw an Ellipse");
		ovalLabel.addMouseListener(this);
		add(ovalLabel);
		
		eraserLabel = new JLabel(eraserIcon);
		eraserLabel.setPreferredSize(new Dimension(w,h));
		eraserLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
		eraserLabel.setToolTipText("Eraser");
		eraserLabel.addMouseListener(this);
		add(eraserLabel);
		
		fillLabel = new JLabel(fillIcon);
		fillLabel.setPreferredSize(new Dimension(w,h));
		fillLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
		fillLabel.setToolTipText("Fill Shape");
		fillLabel.addMouseListener(this);
		add(fillLabel);
		
		prevLabel = selectLabel;
		status = parent.SELECT;
		setPreferredSize(new Dimension(w,h*6));
		setBorder(BorderFactory.createLineBorder(Color.BLACK));
	}
	
	public int getStatus() { return status; }
	
	public void mouseClicked(MouseEvent e){
		JLabel label = (JLabel)e.getSource();
		if(label==fillLabel) {
			if(parent.getFill() == false) {
				label.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
				parent.setFill(true);
			}
			else {
				label.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
				parent.setFill(false);
			}
		}
		else {
			prevLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
			label.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
			prevLabel = label;
			
			if(label==selectLabel)
				status = parent.SELECT;
			else if(label==lineLabel)
				status = parent.LINE;
			else if(label==rectLabel)
				status = parent.RECTANGLE;
			else if(label==ovalLabel)
				status = parent.OVAL;
			else 
				status = parent.ERASER;
			
			parent.newStatus(status);
		}
	}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
}